package com.sdyeda.dtu.net;

import java.net.Socket;
import java.util.ArrayList;
import java.util.List;

public class DtuInfo {

	private Socket socket;
	private String id;
	private String ip;
	private int port;
	private List<IotInfo> iots;
	
	public DtuInfo(String id,String ip,int port) {
		setId(id);
		setIp(ip);
		setPort(port);
	}
	
	public DtuInfo(Socket socket) {
		setSocket(socket);
		iots = new ArrayList<IotInfo>();
	}
	
	public Socket getSocket() {
		return socket;
	}
	public void setSocket(Socket socket) {
		this.socket = socket;
		if(socket !=null) {
			//setId(socket.getInetAddress().getHostName());
			setIp(socket.getInetAddress().getHostAddress());
			setPort(socket.getPort());
		}
	}
	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getIp() {
		return ip;
	}
	public void setIp(String ip) {
		this.ip = ip;
	}
	public int getPort() {
		return port;
	}
	public void setPort(int port) {
		this.port = port;
	}

	public List<IotInfo> getIots() {
		return iots;
	}

	public void setIots(List<IotInfo> iots) {
		this.iots = iots;
	}

	public void updateIot(IotInfo iot) {
		boolean find = false;
		for(int i=0;i<getIots().size();i++) {
			if(getIots().get(i).getId().equals(iot.getId())) {
				find = true;
			}
		}
		if(!find) {
			getIots().add(iot);
		}
	}
}
